package utls;

import People.patient;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;

public class ReadCVS {

    public static void main(String[] args) {

        ReadCVS obj = new ReadCVS();
        obj.run();

    }

    public void run() {

       final Collection<patient> pats = new ArrayList<patient> ();
        String csvFile = "/Users/BaN/Desktop/fucklatest.csv";
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ",";

        try {

            br = new BufferedReader(new FileReader(csvFile));
            while ((line = br.readLine()) != null) {

                try{
                    
              
                // use comma as separator
                String[] input = line.split(cvsSplitBy);
                System.out.println("input " + line);
                System.out.println(input.length);

                patient p = new patient();

              
                for (int index = 0; index < 25; index++) {
               
                    if (!input[index].contains("NULL") && input[index].length() > 1) {
                        switch (index) {
                            case 0: {
                                p.setRecordId(Integer.parseInt(input[index]));
                            }
                            break;
                            case 1: {
                                p.setAddress1(input[index]);
                            }
                            break;
                            case 2: {
                                p.setAddress2(input[index]);
                            }
                            break;
                            case 3: {
                                p.setCity(input[index]);
                            }
                            break;
                            case 4: {
                                p.setPhone(input[index]);
                            }
                            break;
                            case 5: {
                                p.setFirstName(input[index]);
                            }
                            break;
                            case 6: {
                                p.setLastName(input[index]);
                            }
                            break;
                            case 7: {
                                p.setMiddleName(input[index]);
                            }
                            break;
                            case 8: {
                                
//                                p.setCreated((Date.valueOf(input[index]).getTime()));
                            }
                            break;
                            case 9: {
                                try{
                                       input[index] = input[index].trim();
                                if (input[index].length() > 3) {
                                    p.setNationalID(Integer.parseInt(input[index]));
                                }
                                }catch(Exception e)
                                {
                                    e.printStackTrace();;
                                }
                             

                            }
                            break;
                            case 10: {
                                p.setSex(input[index]);
                            }
                            break;
                            case 11: {
//                                p.setBdate((Date.valueOf(input[index]).getTime()));
                            }
                            break;
                            case 12: {
                                p.setWorkPhone(input[index]);
                            }
                            break;
                            case 13: {
                                p.setMobile(input[index]);
                            }
                            break;
                            case 14: {
                                p.setPobox(input[index]);
                            }
                            break;
                            case 15: {
                                p.setNationality((input[index]));
                            }
                            break;
                            case 16: {
                                p.setMarried((input[index]));
                            }
                            break;
                            case 17: {
                            }
                            break;
                            case 18: {
                            }
                            break;
                            case 19: {
                                p.setNotes(input[index]);
                            }
                            break;
                            case 20: {
                            }
                            break;
                            case 21: {
                            }
                            break;
                            case 22: {
                                p.setArabicName(input[index]);
                            }
                            break;
                            case 23: {
                                p.setEmail(input[index]);
                            }
                            break;
                            case 24: {
//                                p.setLast_updated((Date.valueOf(input[index]).getTime()));
                            }
                            break;
                            case 25: {
                            }
                            break;
                            case 26: {
                            }
                            break;
                            case 27: {
                            }
                        }
                        
                    }

                }
pats.add(p);

  }catch(Exception e)
  {
      System.out.print("error");
  }
            }

        } catch (FileNotFoundException e) {
                        System.out.println("fuck");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("shit");
            e.printStackTrace();
        } finally {
            System.out.println("size = " + pats.size());
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        new JFXPanel(); // initializes JavaFX env
        final Application app = new Application() {
            @Override
            public void start(Stage stage) throws Exception {

                TableView<patient> table = new TableView<patient>();
                ObservableList<patient> data =
                        FXCollections.observableArrayList(pats);

                Scene scene = new Scene(new Group());
                stage.setTitle("Table View Sample");
                stage.setWidth(450);
                stage.setHeight(550);

                final Label label = new Label("View Patients");
                label.setFont(new Font("Arial", 20));

                table.setEditable(true);

                TableColumn firstNameCol = new TableColumn("First Name");
                firstNameCol.setMinWidth(100);
                firstNameCol.setCellValueFactory(
                        new PropertyValueFactory<patient, String>("firstName"));

                TableColumn lastNameCol = new TableColumn("Last Name");
                lastNameCol.setMinWidth(100);
                lastNameCol.setCellValueFactory(
                        new PropertyValueFactory<patient, String>("lastName"));

                TableColumn emailCol = new TableColumn("email");
                emailCol.setMinWidth(200);
                emailCol.setCellValueFactory(
                        new PropertyValueFactory<patient, String>("email"));

                table.setItems(data);
                table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
                final VBox vbox = new VBox();
                vbox.setSpacing(5);
                vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(label, table);

                ((Group) scene.getRoot()).getChildren().addAll(vbox);

                stage.setScene(scene);
                stage.show();
            }
        }                   ;



        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

                try {
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {

                            try {
                                app.start(new Stage())    ;
                            } catch (Exception e) {
                                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                            }
                        }
                    });
;
                } catch (Exception e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            }
        });


    }




    }
